const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  
  try {
    // 从数据库中获取用户信息
    const userInfo = await db.collection('users')
      .where({
        _openid: wxContext.OPENID
      })
      .get()
    
    return {
      success: true,
      data: userInfo.data[0] || null
    }
  } catch (err) {
    return {
      success: false,
      error: err
    }
  }
} 